import Vue from 'vue'
import Router from 'vue-router'
import store from '../store'
import Index from '@/components/index'
import ShopDetail from '@/components/shop-detail' //商品详情页
import UpShopDetail from '@/components/upshopdetail' //升级商品详情页
import ShopDetailUP from '@/components/shopDetailUP' //升级商品展示
import Adress from '@/components/adress' //地址
import Personal from '@/components/personal' //个人中心
import AddAdress from '@/components/addadress' //添加地址
import Order from '@/components/order' //订单
import OrderDetail from '@/components/orderdetail' //订单详情
import SureOrder from '@/components/sureOrder'
import Suggestion from '@/components/suggestion' //意见反馈
import MyWallet from '@/components/mywallet' //我的钱包
import Agent from '@/components/agent' //申请成为全民经纪人
import Lottery from '@/components/lottery' //抽奖
import ProductList from '@/components/productList' //分类列表
import GoodsList from '@/components/goodsList' //商品列表
import AllClass from '@/components/allclass' //全部分类
import AboutPlay from '@/components/aboutPlay' //关于玩法
import AboutShop from '@/components/aboutShop' //关于商城
import WidthDraw from '@/components/widthDraw' //关于商城
import Errorpage from '@/components/error' //
import Coupon from '@/components/coupon' //优惠卷
import Cart from '@/components/cart' //购物车
import OrderList from '@/components/orderList' //购物车订单
import Loading from '@/components/loading' //购物车订单
Vue.use(Router)


 const routes=[{
        path: '/',
        name: 'shouye',
        meta: {
            title: '首页'
        },
        component: Index
    }, {
        path: '/ShopDetail',
        name: 'ShopDetail',
        meta: {
            requireAuth: true,
            title: "商品详情页"
        },
        component: ShopDetail //ShopDetailUP 
    }, {
        path: '/Loading',
        name: 'loading',
        meta: {
            title: "超级购"
        },
        component: Loading //ShopDetailUP 
    },{
        path: '/OrderList',
        name: 'OrderList',
        meta: {
            requireAuth: true,
            title: "订单详情"
        },
        component: OrderList //ShopDetailUP 
    },{
        path: '/GoodsList',
        name: 'GoodsList',
        meta: {
            requireAuth: true,
            title: "商品列表"
        },
        component: GoodsList
    }, {
        path: '/ShopDetailUP',
        name: 'ShopDetailUP',
        meta: {
            requireAuth: true,
            title: "升级商品"
        },
        component: ShopDetailUP
    }, {
        path: '/ProductList',
        name: 'ProductList',
        meta: {
            requireAuth: true,
            title: "全部分类"
        },
        component: ProductList
    }, {
        path: '/UpShopDetail',
        name: 'UpShopDetail',
        meta: {
            requireAuth: true,
            title: "升级商品"
        },
        component: UpShopDetail
    }, {
        path: '/Adress',
        name: 'Adress',
        meta: {
            requireAuth: true,
            title: "地址"
        },
        component: Adress
    }, {
        path: '/AddAdress',
        name: 'AddAdress',
        meta: {
            requireAuth: true,
            title: "添加地址"
        },
        component: AddAdress
    }, {
        path: '/Order',
        name: 'dingdan',
        meta: {
            requireAuth: true,
            title: "订单"
        },
        component: Order
    }, {
        path: '/OrderDetail',
        name: 'OrderDetail',
        meta: {
            requireAuth: true,
            title: "订单详情"
        },
        component: OrderDetail
    }, {
        path: '/Personal',
        name: 'wode',
        meta: {
            requireAuth: true,
            title: "个人中心"
        },
        component: Personal
    }, {
        path: '/Suggestion',
        name: 'Suggestion',
        meta: {
            requireAuth: true,
            title: "意见反馈"
        },
        component: Suggestion
    }, {
        path: '/MyWallet',
        name: 'MyWallet',
        meta: {
            requireAuth: true,
            title: "我的钱包"
        },
        component: MyWallet
    }, {
        path: '/Agent',
        name: 'Agent',
        meta: {
            requireAuth: true,
            title: "申请成为全民经纪人"
        },
        component: Agent
    }, {
        path: '/Lottery',
        name: 'kaijiang',
        meta: {
            requireAuth: true,
            title: "抽奖"
        },
        component: Lottery
    }, {
        path: '/AllClass',
        name: 'fenlei',
        meta: {

            title: "全部分类"
        },
        component: AllClass
    }, {
        path: '/AboutPlay',
        name: 'AboutPlay',
        meta: {
            title: "关于玩法"
        },
        component: AboutPlay
    }, {
        path: '/AboutShop',
        name: 'AboutShop',
        meta: {
            title: "关于商城"
        },
        component: AboutShop
    }, {
        path: '/SureOrder',
        name: 'SureOrder',
        meta: {
            requireAuth: true,
            title: "确认订单"
        },
        component: SureOrder
    }, {
        path: '/WidthDraw',
        name: 'WidthDraw',
        meta: {
            requireAuth: true,
            title: "申请提现"
        },
        component: WidthDraw
    }, {  
        path: '/Error',
        name: 'Error',
        meta: {
            title: "服务器错误"
        },
        component: Errorpage
    },//Cart
    { 
        path: '/Cart',
        name: 'Cart',
        meta: {
            title: "购物车",
            requireAuth: true,
        },
        component:Cart
    },
    { 
        path: '/Coupon',
        name: 'Coupon',
        meta: {
            requireAuth: true,
            title: "优惠卷"
        },
        component:Coupon
    }]
    if (window.localStorage.getItem('tokens')) {
        store.commit('LOGIN',window.localStorage.getItem('tokens'))
    }

    const router = new Router({
        routes
    });
router.beforeEach((to, from, next) => {
    if (to.matched.some(r => r.meta.requireAuth)) {
         const data=to.path.replace("/","");
        if (store.state.token) {
            next();
        }else {

            //window.location.href='http://localhost:8080/#/Personal?userId='+"1"
             window.location.href='http://www.chenyekeji.com/cy_market/device/pay/redirect?url='+to.path
        //    next();
            
        }
    }
    else {
        next();
    }
})
    export default router;